<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ChatGPT Stream Output</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #f4f4f4;
        }

        #chat-box {
            width: 100%;
            max-width: 600px;
            height: 600px;
            border: 1px solid #ccc;
            background-color: #fff;
            padding: 10px;
            overflow-y: auto;
            margin-bottom: 10px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        #chat-box p {
            margin: 5px 0;
            padding: 8px;
            border-radius: 5px;
            max-width: 70%;
        }

        .user-message {
            text-align: left;
            background-color: #e9ecef;
            color: #0a0a0a;
            display: block;
        }

        .ai-message {
            text-align: right;
            background-color: #e9ecef;
            color: #0a0a0a;
            display: inline-block;
        }

        #user-input {
            width: calc(100% - 22px);
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            margin-bottom: 10px;
        }

        #send-btn {
            padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        #send-btn:hover {
            background-color: #0056b3;
        }
    </style>
</head>

<body>
    <h1>ChatGPT Stream Output</h1>
    <div id="chat-box"></div>
    <input type="text" id="user-input" placeholder="Type your message here...">
    <button id="send-btn">Send</button>

    <script>
        const chatBox = document.getElementById('chat-box');
        const userInput = document.getElementById('user-input');
        const sendBtn = document.getElementById('send-btn');

        sendBtn.addEventListener('click', () => {
            const message = userInput.value.trim();
            if (message) {


                // 显示用户消息
                const userMessageElement = document.createElement('span');
                userMessageElement.textContent = message;
                userMessageElement.className = 'user-message';
                chatBox.appendChild(userMessageElement);

                const data = {
                    messages: [{
                        content: message,
                        conversationId: 108,
                        messageId: "hh3q6b8q61g745lmbtz3",
                        role: "user"
                    }]
                };
                fetch('http://10.1.196.42:10086/api/kimi/chat2GPT4stream', { // 替换为你的后端接口地址
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                }).then(response => {
                    var reader = response.body.getReader();
                    // 处理流的方法
                    function processStream({ done, value }) {
                        if (done) {
                            console.log("done");
                            return;
                        }
                        var text = new TextDecoder("utf-8").decode(value);
                        const m = text.split(",")
                        const tempStr = m[0]
                        // let resStr = tempStr.slice(16, tempStr.length -1)
                        let newContent = ''
                        try {
                            console.log("---", tempStr)
                            newContent = tempStr //JSON.parse(`"${tempStr}"`);
                            // 成功解析后的代码
                        } catch (e) {
                            if (e instanceof SyntaxError) {
                                // 处理 JSON 解析错误
                                console.log("JSON解析错误：", e.message);
                            } else {
                                // 处理其他可能的错误
                                throw e;
                            }
                        }
                        document.body.appendChild(document.createElement('br'));
                        // 显示 AI 消息
                        const aiMessageElement = document.createElement('p');
                        aiMessageElement.textContent = newContent.replace(/<NEWLINE>/g, '\n');
                        aiMessageElement.className = 'ai-message';
                        chatBox.appendChild(aiMessageElement);
                        return reader.read().then(processStream)
                    }
                    return reader.read().then(processStream);
                });
                // 清空输入框
                userInput.value = '';
                chatBox.scrollTop = chatBox.scrollHeight; // 自动滚动到底部
            }
        });
    </script>
</body>

</html>